package mapEditor.view.resItemEdit
{
	import asset.mapEditorItemAssetListToolAsset;
	import asset.mapEditorItemAssetListViewAsset;
	
	import common.AlertView;
	
	import fl.containers.ScrollPane;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mapEditor.MapEditorManager;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.vo.ResItemItemVO;
	import mapEditor.vo.ResItemVO;
	
	import stages.StageReflection;
	
	import utils.DisplayUtil;

	public class ItemPropertyAssetView extends mapEditorItemAssetListViewAsset
	{
		private var _itemAssetListToolAsset:mapEditorItemAssetListToolAsset;
		private var _resItemVO:ResItemVO;
		private var _items:Vector.<ItemPropertyAssetItemView>=new Vector.<ItemPropertyAssetItemView>();
		private var _scrollPane:ScrollPane;
		private var _selectItem:ItemPropertyAssetItemView;
		/**
		 *当前是否多选模式 
		 */		
		private var _isMultiSelect:Boolean=false;
		
		public function ItemPropertyAssetView()
		{
			_scrollPane=new ScrollPane();
			_scrollPane.x=posPanel.x;
			_scrollPane.y=posPanel.y;
			_scrollPane.width=180;
			posPanel.x=0;
			posPanel.y=0;
			_scrollPane.source=posPanel;
			addChild(_scrollPane);
			
			_itemAssetListToolAsset=new mapEditorItemAssetListToolAsset();
			_itemAssetListToolAsset.x=(width-_itemAssetListToolAsset.width)/2;
			_itemAssetListToolAsset.y=_scrollPane.y+_scrollPane.height+10;
			_itemAssetListToolAsset.btnDel.buttonMode=_itemAssetListToolAsset.btnVisible.buttonMode=true;
			_itemAssetListToolAsset.btnDel.addEventListener(MouseEvent.CLICK, __onItemAssetListToolAssetMouseEvent);
			_itemAssetListToolAsset.btnVisible.addEventListener(MouseEvent.CLICK, __onItemAssetListToolAssetMouseEvent);
			addChild(_itemAssetListToolAsset);
			
			MapEditorManager.instance.addEventListener(MapEditorEvent.RES_ITEM_ITEM_SELECTED, __onItemSelected);
			StageReflection.stage.addEventListener(Event.RESIZE, __onStageResize);
			setList();
		}
		
		private function __onResItemAddItem(evt:MapEditorEvent):void
		{
			var resItemItemVO:ResItemItemVO=evt.date as ResItemItemVO;
			var itemPropertyAssetItemView:ItemPropertyAssetItemView=new ItemPropertyAssetItemView(resItemItemVO);
			itemPropertyAssetItemView.addEventListener(Event.SELECT, __onItemAssetItemSelect);
			itemPropertyAssetItemView.y=_items.length*itemPropertyAssetItemView.height;
			posPanel.addChild(itemPropertyAssetItemView);
			_items.push(itemPropertyAssetItemView);
			setScrollPane();
			_scrollPane.refreshPane();
			_scrollPane.update();
		}		
		
		private function __onItemRemove(evt:MapEditorEvent):void
		{
			setList();
		}
		
		private function __onItemAssetListToolAssetMouseEvent(evt:MouseEvent):void
		{
			switch(evt.currentTarget)
			{
				case _itemAssetListToolAsset.btnDel:
					var alert:AlertView=new AlertView("确定要删除此项资源？");
					alert.addEventListener(Event.COMPLETE, __onDelAlertEvent);
					alert.addEventListener(Event.CANCEL, __onDelAlertEvent);
					alert.show();
					break;
				case _itemAssetListToolAsset.btnVisible:
					_selectItem.resItemItemVO.visible=!_selectItem.visible;
					_selectItem.resItemItemVO.change();
					break;
			}
		}
		
		private function __onDelAlertEvent(evt:Event):void
		{
			var alert:AlertView=evt.currentTarget as AlertView;
			switch(evt.type)
			{
				case Event.COMPLETE:
					_resItemVO.removeItem(_selectItem.resItemItemVO);
					_selectItem=null;
					break;
			}
			alert.removeEventListener(Event.COMPLETE, __onDelAlertEvent);
			alert.removeEventListener(Event.CANCEL, __onDelAlertEvent);
			alert.dispose();
			alert=null;
		}
		
		private function __onItemSelected(evt:MapEditorEvent):void
		{
			if(_resItemVO)
			{
				_resItemVO.removeEventListener(MapEditorEvent.RES_ITEM_PROPERTY_CHANGE, __onPropertyChange);
				_resItemVO.removeEventListener(MapEditorEvent.RES_ITEM_REMOVE_ITEM, __onItemRemove);
				_resItemVO.removeEventListener(MapEditorEvent.RES_ITEM_ADD_ITEM, __onResItemAddItem);
				_resItemVO=null;
			}
			
			_resItemVO=evt.date as ResItemVO;
			_resItemVO.addEventListener(MapEditorEvent.RES_ITEM_PROPERTY_CHANGE, __onPropertyChange);
			_resItemVO.addEventListener(MapEditorEvent.RES_ITEM_REMOVE_ITEM, __onItemRemove);
			_resItemVO.addEventListener(MapEditorEvent.RES_ITEM_ADD_ITEM, __onResItemAddItem);
			setList();
		}
		
		private function __onPropertyChange(evt:MapEditorEvent):void
		{
			setList();
		}
	
		private function setList():void
		{
			clearList();
			if(!_resItemVO)return;
			for each(var resItemItemVO:ResItemItemVO in _resItemVO.items)
			{
				var itemPropertyAssetItemView:ItemPropertyAssetItemView=new ItemPropertyAssetItemView(resItemItemVO);
				itemPropertyAssetItemView.addEventListener(Event.SELECT, __onItemAssetItemSelect);
				itemPropertyAssetItemView.y=_items.length*itemPropertyAssetItemView.height;
				posPanel.addChild(itemPropertyAssetItemView);
				_items.push(itemPropertyAssetItemView);
			}
			setScrollPane();
			_scrollPane.refreshPane();
			_scrollPane.update();
		}
		
		private function __onItemAssetItemSelect(evt:Event):void
		{
			if(_selectItem==evt.currentTarget as ItemPropertyAssetItemView)return;
			if(_selectItem && !_isMultiSelect)
			{
				_selectItem.resItemItemVO.selected=false;
				_selectItem.resItemItemVO.change();
			}
			_selectItem=evt.currentTarget as ItemPropertyAssetItemView ;
		}
		
		private function clearList():void
		{
			while(_items && _items.length>0)
			{
				var itemPropertyAssetItemView:ItemPropertyAssetItemView=_items.shift();
				itemPropertyAssetItemView.removeEventListener(Event.SELECT, __onItemAssetItemSelect);
				itemPropertyAssetItemView.dispose();
				itemPropertyAssetItemView=null;
			}
		}
		
		private function setScrollPane():void
		{
			_scrollPane.height=StageReflection.stageHeight-280-35-31;
			_itemAssetListToolAsset.y=_scrollPane.y+_scrollPane.height+10;
		}
		
		private function __onStageResize(evt:Event):void
		{
			setScrollPane();
		}
		
		public function dispose():void
		{
			MapEditorManager.instance.removeEventListener(MapEditorEvent.RES_ITEM_ITEM_SELECTED, __onItemSelected);
			StageReflection.stage.removeEventListener(Event.RESIZE, __onStageResize);
			_selectItem=null;
			if(_resItemVO)
			{
				_resItemVO.removeEventListener(MapEditorEvent.RES_ITEM_REMOVE_ITEM, __onItemRemove);
				_resItemVO.removeEventListener(MapEditorEvent.RES_ITEM_ADD_ITEM, __onResItemAddItem);
				_resItemVO.removeEventListener(MapEditorEvent.RES_ITEM_PROPERTY_CHANGE, __onPropertyChange);
				_resItemVO=null;
			}
			clearList();
			_items=null;
			if(_itemAssetListToolAsset)
			{
				_itemAssetListToolAsset.btnDel.removeEventListener(MouseEvent.CLICK, __onItemAssetListToolAssetMouseEvent);
				_itemAssetListToolAsset.btnVisible.removeEventListener(MouseEvent.CLICK, __onItemAssetListToolAssetMouseEvent);
				DisplayUtil.dispose(_itemAssetListToolAsset);
				_itemAssetListToolAsset=null;
			}
			DisplayUtil.dispose(_scrollPane);
			_scrollPane=null;
			if(parent)parent.removeChild(this);
		}
	}
}